<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>无标题文档</title>
</head>

<body>
<?php
	
	define('WALLTECH_SERVER', 'http://qa.etowertech.com');
	define('ACCESS_TOKEN', 'test5AdbzO5OEeOpvgAVXUFE0A');//testFuSRDt8heEeOpvgAVXUFE98
	define('SECRET_KEY', '79db9e5OEeOpvgAVXUFWSD');
	
	function build_headers($method, $path, $acceptType='application/json'){
		$walltech_date=date(DATE_RSS);
		$auth = $method."\n".$walltech_date."\n".$path;
		$hash=base64_encode(hash_hmac('sha1', $auth, SECRET_KEY, true));
		
		//echo $walltech_date."<br>".$auth."<br>".$hash."<br>";
		return array(	'Content-Type: application/json',
						'Accept: '.$acceptType,
						'X-WallTech-Date: '.$walltech_date,
						'Authorization: WallTech '.ACCESS_TOKEN.':'.$hash);
	}
	
	function send_request($method,$headers,$body){
		$ch = curl_init(WALLTECH_SERVER.$method);
		curl_setopt($ch, CURLOPT_CUSTOMREQUEST, 'POST');  
		curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
		
		curl_setopt($ch, CURLOPT_HTTPHEADER,$headers);   
		curl_setopt($ch, CURLOPT_POSTFIELDS, $body);
		return curl_exec($ch);
	}
	
	function create_order(){
		$method='/services/integration/shipper/orders';
		$headers=build_headers('POST', WALLTECH_SERVER.$method);
		$body='[{"referenceNo":"219784278988675","recipientName":"Paul Allan","addressLine1":"123 A Street","city":" PORT BOTANY","state":"NSW","postcode":"2036","weight":0.45,"description":"plastic toy","invoiceValue":9.89},{"referenceNo":"14940690387034863527","recipientName":"Paul Allan","addressLine1":"123 A Street","city":" PORT BOTANY","state":"NSW","postcode":"2036","weight":0.45,"description":"plastic toy","invoiceValue":9.89}]';
		return send_request($method,$headers,$body);
	}

	function print_label(){
		$method='/services/integration/shipper/labels';
		$headers=build_headers('POST', WALLTECH_SERVER.$method,'application/octet-stream');
		$body='["FM201706120289","FM201706130096"]';
		$site_root=$_SERVER['DOCUMENT_ROOT'];
		$img_content= send_request($method,$headers,$body);
        $dir = $site_root. "/uploads/ubiexpress/".date("Ymd");
        
        !is_dir($dir) && @mkdir($dir, 0755, true);
        $Path = $dir. "/aaaaa-order.pdf";
		file_put_contents($Path, $img_content);//base64_decode(
        $stie_path="/uploads/ubiexpress/".date("Ymd")."/aaaaa-order.pdf";
        echo '运单:<a href='.$stie_path.' target="_blank">'.$stie_path.'</a><br />';
	}
	
	function forecast(){
		$method='/services/integration/shipper/manifests';
		$headers=build_headers('POST', WALLTECH_SERVER.$method);
		$body='["JDQ001961301000935102","FM201706130096"]';
        
		return send_request($method,$headers,$body);
	}

	function track(){
                $method='/services/integration/shipper/trackingEvents';
                $headers=build_headers('POST', WALLTECH_SERVER.$method);
                $body='["SHW1005282011420","SHW1005379011425"]';

                return send_request($method,$headers,$body);
        }


	function pod() {
		$method='/services/integration/shipper/proof-of-delivery/3LQ0008086010229';
		$headers=build_headers('GET', WALLTECH_SERVER.$method);
		return send_request_get($method,$headers);
	}

	echo create_order();
    echo "<br /><br /><br /><br /><br /><br /><br /><br /><br /><br /><br /><br />";
	echo print_label();
    echo "<br /><br /><br /><br /><br /><br />";
	echo forecast();
?>